const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:'',
        showMarkInfo:false,
        minDate:new Date(2010, 0, 1).getTime(),
        remarkFormData:{
            id:'',
            markType:-1,
            markTime:'',
            remark:''
        },
        markShow:false,
        positionName:'门诊',
        vacancy:80,
        tableData:[
            {
                equCode:'L6547',
                equType:'手动轮椅',
                unScan:'2次'
            },
            {
                equCode:'L6547',
                equType:'手动轮椅',
                unScan:'2次'
            },
        ],
        columns:[
            { title:'桩位编号',key:'returnHid',width:'150rpx' },
            { title:'桩类型',key:'type',width:'150rpx' },
            { title:'桩闲置率',key:'unUsedRate',width:'150rpx' },
            { title:'设备平均闲置率',key:'avgUnUsedRate',width:'250rpx' },
            { title:'3天未扫码',key:'unUsedThreeDayCount',width:'150rpx' },
            { title:'3天未租借',key:'unUsedThreeDayCount',width:'150rpx' },
            { title:'设备在桩时长',key:'unUsedTimes',width:'200rpx' },
            { title:'统计时间',key:'createDate',width:'150rpx' },
        ],
        date: '',
        show: false,
        onBeforeClose(action, done) {
            if (action == 'confirm') {
                return false
            } else {
                return true
            }
        },
    },
    onDisplay() {
        this.setData({ show: true });
      },
      onClose() {
        this.setData({ show: false });
      },
      formatDate(date) {
        date = new Date(date);
        return `${date.getYear() +1900 }-${date.getMonth() + 1}`;
      },
      onConfirm(event) {
        this.setData({
          show: false,
          date: this.formatDate(event.detail),
        });
      },
    onInput(event) {
        this.setData({
            currentDate:event.detail
        })
    },
    onMarkDisplay() {
        this.setData({ markShow: true });
    },
    onMarkClose() {
        this.setData({ markShow: false });
        console.log(this.data.markShow)
    },
    onMarkConfirm(event) {
        this.data.remarkFormData.markTime = this.formatDate(event.detail)
        this.setData({
          markShow: false,
          remarkFormData: this.data.remarkFormData
        });
     },
    //标记类型
    onChangeMarkType(e) {
        this.data.remarkFormData.markType = e.detail
        this.setData({
            remarkFormData: this.data.remarkFormData
        })
    },
    // 备注
    updateMarkRemark(e) {
        this.data.remarkFormData.remark = e.detail.value
        this.setData({
            remarkFormData: this.data.remarkFormData
        })
    },
   //取消标记
   onCloseContacts() {
        this.resetMarkInfo()
        this.setData({
            showMarkInfo:false,
        })
    },
    showRemark(e) {
        this.resetMarkInfo()
        this.data.remarkFormData.id = e.target.dataset.id
        if(e.target.dataset.marktype){
            this.data.remarkFormData.markType = e.target.dataset.marktype
        } else {
            this.data.remarkFormData.markType = 0
        }
        if(e.target.dataset.marktime){
            this.data.remarkFormData.markTime = e.target.dataset.marktime
        }
        this.setData({
            showMarkInfo:true,
            remarkFormData:this.data.remarkFormData
        })
    },
    resetMarkInfo() {
        this.setData({
            remarkFormData:{
                id:'',
                markType:'',
                markTime:'',
                remark:''
            }
        })
    },
   //确定处理标记
   onConfirmContacts() {
       let that=this
        if (that.data.remarkFormData.markType!=-1 && that.data.remarkFormData.markTime ) {
            let params = that.data.remarkFormData
            wx.showLoading({
              title: '加载中',
            })
            lbbTaskService.markPosition(params,function(res){
                wx.hideLoading()
                that.onCloseContacts()
                that.this.getInfoById()
            })
        } else {
            this.openPrompt()
        }
    },
    //校验提示
    openPrompt() {
        wx.showToast({
            title: '请检查必填项',
            icon: 'error',
            duration: 2000
        })
    },
    getPileDetail(e) {
        wx.navigateTo({
          url: '/pkgCost/pages/ldle-table-detail/index?returnHid='+e.target.dataset.returnHid+'&mechanismId='+e.target.dataset.mechanismId+'&position='+e.target.dataset.position,
        })
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            id:options.id
        })
        this.getInfoById()
    },
    getInfoById() {
        let  that = this
        lbbTaskService.getUnusedPositionDetail({mechanismId:that.data.id},function(res){
            that.setData({
               positionList:res.data 
            })
        })

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})